package cz.data.domain.standard.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.standard.mapstruct.ContrastDictMapper;
import cz.data.domain.standard.model.dto.ContrastDictDto;
import cz.data.domain.standard.model.entity.ContrastDictEntity;
import cz.data.domain.standard.model.query.ContrastDictQuery;
import cz.data.domain.standard.model.vo.ContrastDictVo;
import cz.data.domain.standard.service.ContrastDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"字典对照"})
@RestController
@RequestMapping("/contrastDicts")
public class ContrastDictController extends BaseController {

    @Autowired
    private ContrastDictService contrastDictService;

    @Autowired
    private ContrastDictMapper contrastDictMapper;

    @ApiOperation(value = "字典对照-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ContrastDictVo> getContrastDictById(@PathVariable String id) {
        ContrastDictEntity contrastDictEntity = contrastDictService.getContrastDictById(id);
        return new SimpleDataResponse<>(contrastDictMapper.toVO(contrastDictEntity));
    }

    @ApiOperation(value = "字典对照-分页查询")
    @ApiImplicitParam(name = "contrastDictQuery", value = "查询实体contrastDictQuery", required = true, dataTypeClass = ContrastDictQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ContrastDictVo>> getContrastDictPage(ContrastDictQuery contrastDictQuery) {
        QueryWrapper<ContrastDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(contrastDictQuery.getContrastId()), "d.contrast_id", contrastDictQuery.getContrastId());
        queryWrapper.like(StringUtils.isNotBlank(contrastDictQuery.getColCode()), "d.col_code", contrastDictQuery.getColCode());
        queryWrapper.like(StringUtils.isNotBlank(contrastDictQuery.getColName()), "d.col_name", contrastDictQuery.getColName());
        IPage<ContrastDictEntity> page = contrastDictService.page(new Page<>(contrastDictQuery.getPageNum(), contrastDictQuery.getPageSize()), queryWrapper);
        List<ContrastDictVo> collect = page.getRecords().stream().map(contrastDictMapper::toVO).collect(Collectors.toList());
        JsonPage<ContrastDictVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "字典对照-新增")
    @ApiImplicitParam(name = "contrastDict", value = "详细实体contrastDict", required = true, dataType = "ContrastDictDto")
    @PostMapping()
    public SimpleDataResponse<ContrastDictVo> saveContrastDict(@RequestBody @Validated({ValidationGroups.Insert.class}) ContrastDictDto contrastDict) {
        ContrastDictEntity contrastDictEntity = contrastDictService.saveContrastDict(contrastDict);
        return new SimpleDataResponse<>(contrastDictMapper.toVO(contrastDictEntity));
    }

    @ApiOperation(value = "字典对照-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "contrastDict", value = "详细实体contrastDict", required = true, dataType = "ContrastDictDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<ContrastDictVo> updateContrastDict(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ContrastDictDto contrastDict) {
        ContrastDictEntity contrastDictEntity = contrastDictService.updateContrastDict(contrastDict);
        return new SimpleDataResponse<>(contrastDictMapper.toVO(contrastDictEntity));
    }

    @ApiOperation(value = "字典对照-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteContrastDictById(@PathVariable String id) {
        contrastDictService.deleteContrastDictById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "字典对照-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteContrastDictBatch(@PathVariable List<String> ids) {
        contrastDictService.deleteContrastDictBatch(ids);
        return BaseResponse.success();
    }
}
